﻿using gt.framework.Log;
using gt.webapi.core;
using gt.webapi.core.Entity.Client;
using gt.webapi.core.Entity.Options;
using gt.webapi.core.Entity.Throttle;
using Owin;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web.Http;

namespace gt.webapi
{
    public class Startup
    {
        public void Configuration(IAppBuilder appBuilder)
        {
            appBuilder.UseWebApi(ConfigurationWebApi());
        }

        private HttpConfiguration ConfigurationWebApi()
        {
            var config = new HttpConfiguration();
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                    name: "Default",
                    routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );

            var controllerActions = new List<ControllerActionEntity>();
            controllerActions.Add(new ControllerActionEntity("test", "ok"));
            controllerActions.Add(new ControllerActionEntity("test", "ok3"));
            controllerActions.Add(new ControllerActionEntity("test", "error1"));
            controllerActions.Add(new ControllerActionEntity("test", "error2"));
            var clients = new List<AuthorityEntity> { new AuthorityEntity() { Name = "test_client", Appkey = "test", Appsecret = "123456", ControllerActions = controllerActions } };

            WebApiCoreOption coreConfig = new WebApiCoreOption
            {
                Authority = new AuthorityOption(clients).UsingBasicAuthentication(),
                //Log = new LogOption(new FileLogWrapper("InfoLog"), new FileLogWrapper("ErrorLog")),
                Log = new LogOption(new TraceLogWrapper("infoLog", SourceLevels.All, new ConsoleTraceListener()), new TraceLogWrapper("errorLog", SourceLevels.All, new ConsoleTraceListener())),
                IpLimit = new IpLimitOption(new List<string> { "127.0.0.1" }),
                RateLimit = new RateLimitOption("test", new Dictionary<RateLimitPeriod, int> { { RateLimitPeriod.Minute, 10 } })
            };

            config.RegisterWebApiCore(coreConfig);

            return config;
        }
    }
}
